package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.system.domain.cm.CmOperLog;
import cn.flying.system.domain.cm.CmOperLogExample;
import cn.flying.system.domain.cm.CmOperLogWithBLOBs;
import cn.flying.system.mapper.cm.CmOperLogMapper;
import cn.flying.system.service.cm.CmOperLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @description: 操作访问日志服务
 * @author: lvyong
 * @date: 2023年05月31日 13:34
 * @version: 1.0
 */
@Service
public class CmOperLogServiceImpl implements CmOperLogService {

    @Resource
    private CmOperLogMapper cmOperLogMapper;

    /**
     * 分页查询操作日志
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<CmOperLogWithBLOBs> getOperLogPageList(PageRequest<CmOperLog> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmOperLog cmOperLog = pageRequest.getParam();
        CmOperLogExample example = new CmOperLogExample();
        CmOperLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmOperLog.getUserId())) {
            criteria.andUserIdEqualTo(cmOperLog.getUserId());
        }
        if (StringUtils.hasText(cmOperLog.getUserName())) {
            criteria.andUserNameEqualTo(cmOperLog.getUserName());
        }
        example.setOrderByClause(" created_time desc ");
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmOperLogWithBLOBs> page = (Page<CmOperLogWithBLOBs>) cmOperLogMapper.selectByExampleWithBLOBs(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }
}
